package com.hds.modules.sys.service.base.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hds.common.utils.PageUtils;
import com.hds.common.utils.Query;
import com.hds.modules.sys.dao.base.EsurerSmsRecordsDao;
import com.hds.modules.sys.dao.base.EsurerUserDao;
import com.hds.modules.sys.entity.esurer.EsurerUserEntity;
import com.hds.modules.sys.service.base.EsurerUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("esurerUserService")
public class EsurerUserServiceImpl extends ServiceImpl<EsurerUserDao, EsurerUserEntity> implements EsurerUserService{

	@Autowired
	private EsurerUserDao userDao;

	@Autowired
	private EsurerSmsRecordsDao recordsDao;

	@Override
	public EsurerUserEntity queryByMobile(String mobile) {
		Map<String, Object> map=new HashMap();
		map.put("mobile", mobile);
		List<EsurerUserEntity> users = userDao.selectByMap(map);
		return users.get(0);
	}

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String username = (String)params.get("username");
		String mobile = (String)params.get("mobile");


		Page<EsurerUserEntity> page = this.selectPage(
				new Query<EsurerUserEntity>(params).getPage(),
				new EntityWrapper<EsurerUserEntity>()
						.like(StringUtils.isNotBlank(username),"username", username)
						.like(StringUtils.isNotBlank(mobile),"mobile", mobile)

		);
		List<EsurerUserEntity> records = page.getRecords();
		for (EsurerUserEntity record: records) {
			record.setNums(recordsDao.getSendNums(record.getId()));
		}

		page.setRecords(records);

		return new PageUtils(page);
	}

}
